package com.xzh.seckill.exception;

import com.xzh.seckill.vo.RespBean;
import com.xzh.seckill.vo.RespBeanEnum;

import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public RespBean ExceptionHandler(Exception e)
    {
        if(e instanceof GlobalException)
        {
            GlobalException ex = (GlobalException) e;
            return RespBean.error(ex.getRespBeanEnum());
        }else if(e instanceof BindException){
            BindException ex = (BindException) e;
            RespBean respbean = RespBean.error(RespBeanEnum.BIND_ERROR);
            respbean.setMessage("参数校验异常："+ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
            return respbean;
        }
        return RespBean.error(RespBeanEnum.ERROR);
    }
}
